package com.txc.cloud.auth.common.security;

import com.txc.common.global.result.Result;
import com.txc.common.global.result.ResultCode;
import com.txc.common.utils.supply.HttpServletResponseUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 没有登陆处理器
 *
 * @ClassName: UnAuthEntryPoint
 * @Author: TXC
 * @Date: 2022-07-29 15:24
 **/
@Component
public class UnAuthEntryPoint implements AuthenticationEntryPoint {
    /**
     * 未通过认证访问受保护的资源时执行
     *
     * @param request       that resulted in an <code>AuthenticationException</code>
     * @param response      so that the user agent can begin authentication
     * @param authException that caused the invocation
     */
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException authException) throws IOException, ServletException {
        HttpServletResponseUtil.out(response, Result.fail(ResultCode.USER_NOT_LOGIN));
    }
}
